package com.pengpeng.rabbitmq.consumer.web;

import com.pengpeng.rabbitmq.bean.MqExchange;
import com.pengpeng.rabbitmq.bean.MqQueue;
import com.pengpeng.rabbitmq.bean.Result;
import com.pengpeng.rabbitmq.consumer.service.RegisterQueueService;
import com.pengpeng.rabbitmq.declare.AmBindDeclare;
import com.pengpeng.rabbitmq.declare.AmExchangeDeclare;
import com.pengpeng.rabbitmq.declare.AmQueueDeclare;
import com.pengpeng.rabbitmq.constants.ExchangeTypeConstants;
import com.pengpeng.rabbitmq.utils.RabbitMQUtils;
import com.pengpeng.rabbitmq.utils.ResultWapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 彭鹏
 * @date 2021/10/19.
 */
@RestController
@RequestMapping(value = "/consumer")
public class ConsumerController {
    @Autowired
    AmExchangeDeclare amExchangeDeclare;

    @Autowired
    AmQueueDeclare amQueueDeclare;

    @Autowired
    AmBindDeclare amBindDeclare;

    @Autowired
    RegisterQueueService registerQueueService;

    @Autowired
    RabbitMQUtils rabbitMQUtils;

    /**
     * 动态注册交换机
     *
     * @param mqExchage
     * @return
     */
    @PostMapping(value = "/declareDirectExchange")
    public Result declareDirectExchange(@RequestBody MqExchange mqExchage) {
        mqExchage.setType(ExchangeTypeConstants.DIRECT);
        this.amExchangeDeclare.declareExchange(mqExchage);
        return ResultWapper.success();
    }

    @PostMapping(value = "/declareExchange")
    public Result declareExchange(@RequestBody MqExchange mqExchage) {
        this.amExchangeDeclare.declareExchange(mqExchage);
        return ResultWapper.success();
    }

    @PostMapping(value = "/deleteExchange")
    public Result deleteExchange(@RequestBody MqExchange mqExchage) {
        this.amExchangeDeclare.deleteExchange(mqExchage.getName());
        return ResultWapper.success();
    }

    /**
     * 动态注册队列
     *
     * @param mqQueue
     * @return
     */
    @PostMapping(value = "/declareQueue")
    public Result declareQueue(@RequestBody MqQueue mqQueue) {
        this.amQueueDeclare.declareQueue(mqQueue);
        return ResultWapper.success();
    }

    @PostMapping(value = "/deleteQueue")
    public Result deleteQueue(@RequestBody MqQueue mqQueue) {
        this.amQueueDeclare.deleteQueue(mqQueue.getName());
        return ResultWapper.success();
    }

    /**
     * 判断对应的queue是否存在
     *
     * @param queueName
     * @return
     */
    @GetMapping(value = "/{queueName}")
    public Result isExistQueue(@PathVariable("queueName") String queueName) {
        return ResultWapper.success(this.amQueueDeclare.isQueueExist(queueName));
    }

    /**
     * 队列绑定
     *
     * @param queue
     * @param exchange
     * @return
     */
    @GetMapping(value = "/bindQueue/{queue}")
    public Result queueBind(@PathVariable("queue") String queue,
                            @RequestParam(value = "exchange", required = false) String exchange) {
        if (StringUtils.isEmpty(exchange)) {
            return ResultWapper.success(this.amBindDeclare.queueBind(queue));
        } else {
            return ResultWapper.success(this.amBindDeclare.queueBind(queue, exchange, queue));
        }
    }

    /**
     * 注册队列，并且设置该对应的监听
     *
     * @param queue
     * @return
     */
    @GetMapping(value = "registerQueue")
    public Result listenQueue(@RequestParam("queue") String queue,@RequestParam("exchange") String exchange) {
        if(StringUtils.isEmpty(exchange)){
            this.registerQueueService.queue(queue).exchange(MqExchange.DEFAULT_DIRECT_EXCHANGE);
        }else{
            this.registerQueueService.queue(queue).exchange(exchange);
        }
        this.registerQueueService.registerQueue();
        return ResultWapper.success();
    }

    /**
     * 查询所有的交换机
     *
     * @return
     */
    @GetMapping(value = "/listExchanges")
    public Result listExchange() {
        return ResultWapper.success(this.rabbitMQUtils.listExchanges());
    }
}

